package com.cloudcentral.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudcentral.entity.RadioOnline;
import com.cloudcentral.enums.RadioStatus;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.mapper.RadioOnlineMapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 台站上线记录表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Slf4j
@Service
public class RadioOnlineService extends ServiceImpl<RadioOnlineMapper, RadioOnline> {

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Boolean status(Integer radioId, Boolean status) throws FailureException {
    if (radioId == null) throw new FailureException("缺少台站ID");
    else if (status == null) throw new FailureException("缺少在线状态");
    else {
      RadioStatus.value(radioId, status ? RadioStatus.IDLE : RadioStatus.OFFLINE);
      return save(RadioOnline.builder().radioId(radioId).status(status).build());
    }
  }
}
